import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
import Home from '../views/Home.vue'
import NewsDetails from '../views/NewsDetails.vue'
import Categories from '../views/Categories.vue'
import NewsList from '@/views/NewsList.vue'
import Login from '../views/Login.vue'
import Register from '../views/Register.vue'
import Profile from '../views/Profile.vue'
import Works from '../views/Works.vue'
import UserDetails from '../views/UserDetails.vue'
import EditUserDetails from '../views/EditUserDetails.vue'
import Search from '@/views/Search.vue'
import Like from '@/views/Like.vue'
import Favorite from '../views/Favorite.vue'
import History from '@/views/History.vue'
import Settings from '../views/Settings.vue'
import EditNews from '../views/EditNews.vue'
import EditNews2 from '../views/EditNews2.vue'
import Notifications from '../views/Notifications.vue'


import api from '@/api';
import { NewsStore } from '@/stores/newsInfoStore';
import { UserStore } from '@/stores/userInfoStore';


const router = createRouter({
  publicPath:'./',  // 执行 npm run build 统一配置路径
  base: '/',
  history: createWebHashHistory(import.meta.env.BASE_URL),  // 使用hash模式
  //history: createWebHistory(import.meta.env.BASE_URL),
  scrollBehavior(to, from, savedPosition) { //历史滚动位置savedPosition
    if (to.path != '/Home') { // 如果不是首页，则滚动到顶部
      return { top: 0 }
    }
  },
  routes: [
    {
      path: '/',
      name: 'index',
      redirect: '/Home',
    },
    {
      name: '404',
      path: '/:catchAll(.*)',
      component: () => import(`@/views/error/404.vue`)
    },
    {
      path: '/Home',
      name: 'Home',
      component: Home,
    },
    {
      path: '/Notifications',
      name: 'Notifications',
      component: Notifications,
    },
    {
      path: '/NewsDetails',
      name: 'NewsDetails',
      component: NewsDetails,
    },
    {
      path: '/NewsList',
      name: 'NewsList',
      component: NewsList,
    },
    {
      path: '/Categories',
      name: 'Categories',
      component: Categories,
    },
    {
      path: '/Search',
      name: 'Search',
      component: Search,
    },
    {
      path: '/Login',
      name: 'Login',
      component: Login,
    },
    {
      path: '/Register',
      name: 'Register',
      component: Register,
    },
    {
      path: '/Profile',
      name: 'Profile',
      component: Profile,
    },
    {
      path: '/Works',
      name: 'Works',
      component: Works,
    },
    {
      path: '/UserDetails',
      name: 'UserDetails',
      component: UserDetails,
    },
    {
      path: '/EditUserDetails',
      name: 'EditUserDetails',
      component: EditUserDetails,
    },
    {
      path: '/Favorite',
      name: 'Favorite',
      component: Favorite,
    },
    {
      path: '/Like',
      name: 'Like',
      component: Like,
    },
    {
      path: '/History',
      name: 'History',
      component: History,
    },
    {
      path: '/Settings',
      name: 'Settings',
      component: Settings,
    },
    {
      path: '/EditNews',
      name: 'EditNews',
      component: EditNews,
    },
    {
      path: '/EditNews2',
      name: 'EditNews2',
      component: EditNews2,
    },
  ],
})


// 注册全局前置守卫
router.beforeEach(async(to, from, next) => {

  const newsStoreObject = NewsStore(); //这句代码不能放函数外面，要放在路由守卫内部，要等pinia初始化完毕
  const userStoreObject = UserStore();

  // 如果无新闻分类信息缓存
  if(newsStoreObject.newsCategoryInfo.length == 0){
    let res = await api.getAllNewsCategory().then(); //获取所有新闻分类
    newsStoreObject.setNewsCategoryInfo(res.data.data) //将数据缓存
  }
  // 如果无用户信息缓存
  if(userStoreObject.userInfo.id == null){
    //1.从jwt中获取用户信息
    let jwtToken = localStorage.getItem('wx_access-client');
    
    if(jwtToken != null){
      let res =  await api.ParseJwt(jwtToken).then();
      if(res.data.code == 1){
        //2.如果用户登录了，就将用户信息存入pinia仓库
        userStoreObject.setUserInfo(res.data.data);
      }
    }
  }

  next();
})

export default router
